package com.ler.yrmall.product.controller;

import com.ler.yrmall.basic.Result;
import com.ler.yrmall.product.entity.CategoryEntity;
import com.ler.yrmall.product.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/tree")
    public Result<?> listTree() {
        return Result.ok(categoryService.listWithTree());
    }

    @GetMapping("/getCategoryPathById")
    public Result<?> getCategoryPathById(Long categoryId) {
        return Result.ok(categoryService.getCategoryPath(categoryId));
    }


    @RequestMapping("/getById")
    public Result<?> getById(long id) {
        return Result.ok(categoryService.getById(id));
    }

    @RequestMapping("/save")
    public Result<?> save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return Result.ok();
    }

    @RequestMapping("/update")
    public Result<?> update(@RequestBody CategoryEntity category) {
        categoryService.update(category);
        return Result.ok();
    }

    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody List<Long> catIds) {
        categoryService.removeMenuByIds(catIds);
        return Result.ok();
    }

    // 移动端接口
    @GetMapping("/level1")
    public Result<?> categoryLevelOne() {
        return Result.ok(categoryService.getLevelOne());
    }

    @GetMapping("/tree/{lv1Id}")
    public Result<?> categoryLevelOne(@PathVariable("lv1Id") Long pid) {
        return Result.ok(categoryService.getTreeByPid(pid));
    }

}
